package com.sw.chinesewriteboard.operation;

import android.content.Context;

import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.config.ChineseDataConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChineseDataOperate {

    public enum ArrangeType {
        WORD,
        NAME,
        SYMBOL,
        PIN2WORD
    }


    private static int rowEmpty = 0;

    private Context mContext;

    private static float divison = 1; // 全描，半描，不描

    public ChineseDataOperate(Context context) {
        mContext = context;
        rowEmpty = 0;
        divison = 1;
    }

    public String listToString(ArrangeType arrangeType, List<String> list) {

        final int COLUMN_LENGTH = ChineseDataConfig.TOTAL_LENGTH_COLUMN;
        final int ROW_LENGTH = ChineseDataConfig.TOTAL_LENGTH_ROW;

        StringBuilder temp = new StringBuilder();
        switch (arrangeType) {
            case WORD:
                for (String words : list) {
                    for (int i = 0; i < words.length(); i++) {
                        temp.append(words.charAt(i));
                        for (int j = 0; j < COLUMN_LENGTH - 1; j++) {
                            temp.append(' ');
                        }
                        for (int k = 0; k < rowEmpty; k++) {
                            for (int j = 0; j < COLUMN_LENGTH; j++) {
                                temp.append(' ');
                            }
                        }
                    }
                }
                break;

            case NAME:
                String name;
                if (!list.isEmpty()) {
                    name = list.get(0);
                } else {
                    name = "  ";
                }
                for (int i = 0; i < COLUMN_LENGTH; i++) {
                    temp.append(name);
                    for (int j = 0; j < COLUMN_LENGTH - name.length(); j++) {
                        temp.append(' ');
                    }
                }
                break;
            case SYMBOL:
                for (String words : list) {
                    for (int i = 0; i < words.length(); i++) {
                        temp.append(words.charAt(i));
                        int miao = (int) ((COLUMN_LENGTH - 1) * divison);
                        for (int j = 0; j < miao; j++) {
                            temp.append(words.charAt(i));
                        }
                        for (int l = 0; l < COLUMN_LENGTH - 1 - miao; l++) {
                            temp.append(" ");
                        }
                        for (int k = 0; k < rowEmpty; k++) {
                            for (int j = 0; j < COLUMN_LENGTH; j++) {
                                temp.append(' ');
                            }
                        }
                    }
                }
                break;

            case PIN2WORD:
                for (String words : list) {
                    temp.append(words);
                }
                break;
        }

        // 字帖页面上的剩余空格补足
        int surplus = ChineseDataConfig.TOTAL_LENGTH - temp.length();
        addSpace(surplus, temp);
        return temp.toString();
    }

    /*
    往后增加空格
     */
    private StringBuilder addSpace(int surplus, StringBuilder temp) {
        while (surplus > 0) {
            temp.append(' ');
            surplus--;
        }
        return temp;
    }


    public int getRowEmpty() {
        return rowEmpty;
    }

    public static void setRowEmpty(int rowEmpty) {
        ChineseDataOperate.rowEmpty = rowEmpty;
    }

    /*
        检查是否有标点符号
         */
    public boolean checkHasPunctuation(String s) {
        boolean b = false;

        String tmp = s;
        tmp = tmp.replaceAll("\\p{P}", "");
        if (s.length() != tmp.length()) {
            b = true;
        }

        return b;
    }

    public static float getDivison() {
        return divison;
    }

    public static void setDivison(float divison) {
        ChineseDataOperate.divison = divison;
    }

    private Map<String, Integer> symbolMap = new HashMap<>();

    public int getSymbol(String symbol) {
        if (symbolMap.size() == 0) {
            symbolMap.put("a", R.mipmap.aa_bishun_a);
            symbolMap.put("b", R.mipmap.aa_bishun_b);
            symbolMap.put("c", R.mipmap.aa_bishun_c);
            symbolMap.put("d", R.mipmap.aa_bishun_d);
            symbolMap.put("e", R.mipmap.aa_bishun_e);
            symbolMap.put("f", R.mipmap.aa_bishun_f);
            symbolMap.put("g", R.mipmap.aa_bishun_g);
            symbolMap.put("h", R.mipmap.aa_bishun_h);
            symbolMap.put("i", R.mipmap.aa_bishun_i);
            symbolMap.put("j", R.mipmap.aa_bishun_j);
            symbolMap.put("k", R.mipmap.aa_bishun_k);
            symbolMap.put("l", R.mipmap.aa_bishun_l);
            symbolMap.put("m", R.mipmap.aa_bishun_m);
            symbolMap.put("n", R.mipmap.aa_bishun_n);
            symbolMap.put("o", R.mipmap.aa_bishun_o);
            symbolMap.put("p", R.mipmap.aa_bishun_p);
            symbolMap.put("q", R.mipmap.aa_bishun_q);
            symbolMap.put("r", R.mipmap.aa_bishun_r);
            symbolMap.put("s", R.mipmap.aa_bishun_s);
            symbolMap.put("t", R.mipmap.aa_bishun_t);
            symbolMap.put("u", R.mipmap.aa_bishun_u);
            symbolMap.put("v", R.mipmap.aa_bishun_v);
            symbolMap.put("w", R.mipmap.aa_bishun_w);
            symbolMap.put("x", R.mipmap.aa_bishun_x);
            symbolMap.put("y", R.mipmap.aa_bishun_y);
            symbolMap.put("z", R.mipmap.aa_bishun_z);
        }
        return symbolMap.get(symbol);
    }
}
